open Fr
open FrGui_common

let message_box ?parent ?title ?default ?(buttons=[]) child options null =
	let r = ref None in
	let window = GWindow.dialog
		?parent: (match parent with
			| Some parent -> GWindow.toplevel parent#widget
			| None -> None)
		~no_separator: true
		~modal: true
		~type_hint: `DIALOG
		?title () in
	window#vbox#pack ~expand: true child#widget;
	List.iter
		(fun (s, v) ->
			let b = GButton.button
				~label: s
				~packing: window#action_area#pack () in
			ignore (b#connect#clicked
				~callback: (fun () -> r := Some v; window#destroy ()));
			option_may (fun d -> if v = d then b#grab_default ()) default)
		options;
	ignore (window#connect#destroy ~callback:
		(fun () ->
			List.iter window#vbox#remove window#vbox#children;
			GMain.Main.quit ()));
	window#show ();
	GMain.Main.main ();
	match !r with
	| Some v -> v
	| None -> null

(* maybe we should use GtkMessageDialog here? *)
let alert ?parent ?title ?icon ?(okay="Okay") ?buttons message =
	let widget =
		match icon with
		| Some image ->
			FrGui_containers.hbox [
				new simple_widget (gtk_image ~icon_size: `DIALOG image);
				FrGui_widgets.label message ]
		| None ->
			FrGui_widgets.label message
	in
	message_box
		?parent
		?title
		~default: ()
		?buttons
		widget
		[okay, ()] ()
